package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;
import modelo.Facturas;
import persistencia.MetodosFacturas;
import java.util.ArrayList;

/**
 * Panel donde se pintaran las consultas en un JTable
 *
 * @author Antonio David
 */
public class JPConsultaFacturas extends javax.swing.JPanel {

    ResultSet rs;
    ArrayList<Facturas> fa;

    String[] columns = new String[]{
        "IDFactura", "Cliente", "Consumo", "Emisiones", "Eficiencia"
    };

    class MyTableModel extends DefaultTableModel {

        @Override
        public Class getColumnClass(int column) {
            switch (column) {
                case 0:
                case 1:
                    return String.class;
                case 2:
                    return Integer.class;
                case 3:
                    return String.class;
                case 4:
                    return ImageIcon.class;
                default:
                    return Object.class;
            }
        }
    }

    public void actualizarComboFacturas() throws SQLException, ClassNotFoundException {
        jComboFacturas.removeAllItems();
        MetodosFacturas mtf = new MetodosFacturas();
        fa = mtf.getFacturas();
        for (int i = 0; i < fa.size(); i++) {
            jComboFacturas.addItem(fa.get(i).getId_facturas());
        }
    }

    private void rellenarJTable(ResultSet rs) {
        try {
            MyTableModel dtm = new MyTableModel();
            dtm.setColumnIdentifiers(columns);

            Object[] obj;
            while (rs.next()) {
                obj = new Object[5];
                obj[0] = rs.getInt("idFactura");
                obj[1] = rs.getString("cliente");
                obj[2] = rs.getInt("idAlbaran");
                obj[3] = rs.getFloat("Precio total");

                dtm.addRow(obj);

                System.out.println("1111");
            }

            jTable1.setModel(dtm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Creates new form JPConsultas
     */
    public JPConsultaFacturas() throws SQLException, ClassNotFoundException /*throws SQLException, ClassNotFoundException*/ {
        initComponents();
        actualizarComboFacturas();
        //actualizarComboEficiencias();
        jButtonConsultar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
              int idFactura = Integer.parseInt((String) jComboFacturas.getSelectedItem());

               
                  MetodosFacturas mf = null;  
                try {
                    mf = new MetodosFacturas();
                    rs = mf.ConsultaFacturas(idFactura);
                    rellenarJTable(rs);
                    System.out.println("CONSULTA REALIZADA");

                } catch (SQLException ex) {
                    System.err.println("SQLEXCEPTION.");
                    ex.printStackTrace();
                } 
            }

        });
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jButtonConsultar = new javax.swing.JButton();
        label1 = new java.awt.Label();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jComboFacturas = new javax.swing.JComboBox();

        setLayout(new java.awt.GridBagLayout());

        jButtonConsultar.setText("Consultar");
        jButtonConsultar.setToolTipText("");
        jButtonConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 38, 0, 0);
        add(jButtonConsultar, gridBagConstraints);

        label1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        label1.setText("Factura :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 50, 0, 0);
        add(label1, gridBagConstraints);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "IDFacturas", "Cliente", "IDAlbaran", "PrecioTotal"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 12;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 609;
        gridBagConstraints.ipady = 192;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 10, 12, 0);
        add(jScrollPane1, gridBagConstraints);

        jComboFacturas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(jComboFacturas, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultarActionPerformed

    }//GEN-LAST:event_jButtonConsultarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConsultar;
    private javax.swing.JComboBox jComboFacturas;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private java.awt.Label label1;
    // End of variables declaration//GEN-END:variables
}
